import { dialog, Notification } from 'electron';

import { log } from './logger';

/**
 * 对话框工具类
 * 封装 Electron 的内置对话框功能
 */
export class DialogManager {
  /**
   * 显示信息提示框
   */
  async showInfo(title: string, message: string, detail?: string): Promise<void> {
    try {
      await dialog.showMessageBox({
        type: 'info',
        title,
        message,
        detail,
        buttons: ['确定']
      });
    } catch (error) {
      log.error('显示信息对话框失败:', error);
    }
  }

  /**
   * 显示错误提示框
   */
  async showError(title: string, message: string, detail?: string): Promise<void> {
    try {
      await dialog.showMessageBox({
        type: 'error',
        title,
        message,
        detail,
        buttons: ['确定']
      });
    } catch (error) {
      log.error('显示错误对话框失败:', error);
    }
  }

  /**
   * 显示警告提示框
   */
  async showWarning(title: string, message: string, detail?: string): Promise<void> {
    try {
      await dialog.showMessageBox({
        type: 'warning',
        title,
        message,
        detail,
        buttons: ['确定']
      });
    } catch (error) {
      log.error('显示警告对话框失败:', error);
    }
  }

  /**
   * 显示确认对话框
   */
  async showConfirm(
    title: string,
    message: string,
    detail?: string,
    confirmText: string = '确定',
    cancelText: string = '取消'
  ): Promise<boolean> {
    try {
      const result = await dialog.showMessageBox({
        type: 'question',
        title,
        message,
        detail,
        buttons: [cancelText, confirmText],
        defaultId: 1,
        cancelId: 0
      });

      return result.response === 1;
    } catch (error) {
      log.error('显示确认对话框失败:', error);
      return false;
    }
  }

  /**
   * 显示选择对话框（多个选项）
   */
  async showChoice(title: string, message: string, choices: string[], detail?: string): Promise<number> {
    try {
      const result = await dialog.showMessageBox({
        type: 'question',
        title,
        message,
        detail,
        buttons: choices,
        defaultId: 0,
        cancelId: 0
      });

      return result.response;
    } catch (error) {
      log.error('显示选择对话框失败:', error);
      return 0;
    }
  }

  /**
   * 显示简单错误框（不阻塞）
   */
  showErrorBox(title: string, content: string): void {
    try {
      dialog.showErrorBox(title, content);
    } catch (error) {
      log.error('显示错误框失败:', error);
    }
  }

  /**
   * 显示简单提示（只有消息，一个确定按钮）
   */
  async showSimpleMessage(message: string, type: 'info' | 'warning' | 'error' = 'info'): Promise<void> {
    try {
      await dialog.showMessageBox({
        type,
        message,
        buttons: ['确定']
      });
    } catch (error) {
      log.error('显示简单提示失败:', error);
    }
  }

  /**
   * 显示系统通知
   */
  showNotification(
    title: string,
    body: string,
    options?: {
      icon?: string;
      silent?: boolean;
      urgency?: 'normal' | 'critical' | 'low';
    }
  ): void {
    try {
      if (!Notification.isSupported()) {
        log.warn('系统不支持通知功能');
        return;
      }

      const notification = new Notification({
        title,
        body,
        icon: options?.icon,
        silent: options?.silent || false,
        urgency: options?.urgency || 'normal'
      });

      notification.show();

      // 可选：添加点击事件
      notification.on('click', () => {
        log.info('用户点击了通知');
      });
    } catch (error) {
      log.error('显示系统通知失败:', error);
    }
  }

  /**
   * 显示成功提示（绿色图标的简单提示）
   */
  async showSuccess(message: string): Promise<void> {
    try {
      await dialog.showMessageBox({
        type: 'info',
        title: '成功',
        message,
        buttons: ['确定']
      });
    } catch (error) {
      log.error('显示成功提示失败:', error);
    }
  }

  /**
   * 显示快速提示（无标题的简单消息）
   */
  async showQuickTip(message: string): Promise<void> {
    try {
      await dialog.showMessageBox({
        type: 'none',
        message,
        buttons: ['知道了']
      });
    } catch (error) {
      log.error('显示快速提示失败:', error);
    }
  }

  /**
   * 选择文件
   */
  async selectFiles(
    title: string = '选择文件',
    filters?: { name: string; extensions: string[] }[],
    multiSelect: boolean = false
  ): Promise<string[]> {
    try {
      const properties = ['openFile'];
      if (multiSelect) {
        properties.push('multiSelections');
      }

      const result = await dialog.showOpenDialog({
        title,
        filters: filters || [{ name: '所有文件', extensions: ['*'] }],
        properties: properties as any
      });

      return result.canceled ? [] : result.filePaths;
    } catch (error) {
      log.error('选择文件失败:', error);
      return [];
    }
  }

  /**
   * 选择文件夹
   */
  async selectFolder(title: string = '选择文件夹'): Promise<string | null> {
    try {
      const result = await dialog.showOpenDialog({
        title,
        properties: ['openDirectory'] as any
      });

      return result.canceled ? null : result.filePaths[0];
    } catch (error) {
      log.error('选择文件夹失败:', error);
      return null;
    }
  }

  /**
   * 保存文件
   */
  async saveFile(
    title: string = '保存文件',
    defaultPath?: string,
    filters?: { name: string; extensions: string[] }[]
  ): Promise<string | null> {
    try {
      const result = await dialog.showSaveDialog({
        title,
        defaultPath,
        filters: filters || [{ name: '所有文件', extensions: ['*'] }]
      });

      return result.canceled ? null : result.filePath;
    } catch (error) {
      log.error('保存文件失败:', error);
      return null;
    }
  }
}

// 创建单例实例
export const dialogManager = new DialogManager();

// 默认导出
export default dialogManager;
